package iotwisp.school.user.service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import iotwisp.base.cqrs.unitofwork.UseUnitOfWork;
import iotwisp.base.orm.BaseDao;
import org.hibernate.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import iotwisp.school.user.command.role.ChangeRoleEnableSCmd;
import iotwisp.school.user.command.role.CreateRoleSCmd;
import iotwisp.school.user.command.role.DeleteRoleSCmd;
import iotwisp.school.user.command.role.ModifyRoleSCmd;
import iotwisp.school.user.entity.Permission;
import iotwisp.school.user.entity.Role;
import iotwisp.school.user.qo.PermissionQO;
import iotwisp.school.user.qo.RoleQO;

@Service
@Transactional
public class RoleService extends BaseDao<Role, RoleQO> {

	@Autowired
	private PermissionService permissionService;

	@UseUnitOfWork(description = "新增角色", operateType = UseUnitOfWork.OperateType.CREATE)
	public Role createRole(CreateRoleSCmd cmd) {

		// 查出要添加的权限
		PermissionQO permissionQO = new PermissionQO();
		permissionQO.setIds(cmd.getAddPermissionIds());
		List<Permission> list = permissionService.queryList(permissionQO);

		Set<Permission> permissionSet = new HashSet<>();
		for (Permission permission : list) {
			permissionSet.add(permission);
		}

		// 创建角色
		Role role = new Role();
		role.create(cmd, permissionSet);

		// 保存角色
		save(role);

		return role;
	}

	@UseUnitOfWork(description = "修改角色", operateType = UseUnitOfWork.OperateType.UPDATE)
	public Role modifyRole(ModifyRoleSCmd cmd) {

		// 查出要修改的角色
		RoleQO roleQO = new RoleQO();
		roleQO.setId(cmd.getRoleId());
		roleQO.setFetchPermissions(true);
		Role role = queryUnique(roleQO);
		// 修改角色
		role.modify(cmd);

		// 要添加的角色
		if (cmd.getAddPermissionIds() != null && cmd.getAddPermissionIds().size() > 0) {
			PermissionQO permissionQO = new PermissionQO();
			permissionQO.setIds(cmd.getAddPermissionIds());
			List<Permission> addPermissions = permissionService.queryList(permissionQO);
			for (Permission permission : addPermissions) {
				role.getPermissionSet().add(permission);
			}
		}

		// 要移除的角色
		if (cmd.getRemovePermissionIds() != null && cmd.getRemovePermissionIds().size() > 0) {
			PermissionQO permissionQO = new PermissionQO();
			permissionQO.setIds(cmd.getRemovePermissionIds());
			List<Permission> removePermissions = permissionService.queryList(permissionQO);

			for (Permission permission : removePermissions) {
				role.getPermissionSet().remove(permission);
			}
		}

		update(role);
		// 修改角色

		return role;
	}

	@UseUnitOfWork(description = "修改角色状态", operateType = UseUnitOfWork.OperateType.UPDATE)
	public void changeRoleEnable(ChangeRoleEnableSCmd cmd) {

		// 查出角色
		Role role = get(cmd.getRoleId());
		// 修改启用状态
		role.changeEnable(cmd);
		update(role);
	}

	public void deleteRole(DeleteRoleSCmd cmd) {

		// 清空角色与用户的关联

		// 清空角色与权限的关联

		// 删除角色
		Role role = get(cmd.getRoleId());
		delete(role);
	}

	@Override
	protected Criteria buildCriteria(Criteria criteria, RoleQO qo) {
		return criteria;
	}

	@Override
	protected Class<Role> getEntityClass() {
		return Role.class;
	}

}
